package com.yql.controller;

import com.yql.entity.User;
import com.yql.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("login")
    public String login(){
        return "login";
    }

    @PostMapping("login")
    public String login(String uname,String upwd, HttpSession session,Model model){
        User user = userService.login(uname,upwd);
        System.out.println(user);
        if(null!=user){
            if(user.getUserStatus()==1){
                session.setAttribute("user",user);
                System.out.println("登录成功");
                return "redirect:listOrder";
            }
            model.addAttribute("lgError","此用户已被禁用，请联系管理员");
            return "login";
        }
        model.addAttribute("lgError","登录失败，检查用户名和密码是否输入正确");
        return "login";
    }
}
